When an error occurs in a procedure that does not have error handling enabled, VB responds by displaying an error message and terminates the application. While this behavior may be acceptable when you are writing and debugging code, it is never acceptable when your users are running your application.
Basic error trapping
VB provides the On Error Goto statement to redirect the execution of the program to a local error handler when an error is encountered.
Sub MyProcedure()
On Error GoTo ErrorHandler
' Some code.
Exit Sub
ErrorHandler:
' Handle the error here.
Select Case vbwErrorHandler(Err.Number, Err.Description, VBWPROJECT, VBWMODULE, VBWPROCEDURE, Erl)
Case vbwEnd
End
Case vbwRetry
Resume
Case vbwIgnoreLine
Resume Next
End Select
End Sub
Almost every procedure should contain an error handler. Yet the handling code is identical in every procedure, and may be quite large for powerful error handling. So instead of writing huge pieces of identical error handling code inside each procedure, it is better to just call a global procedure from there, known as a global error handler.
VB Watch Protector automates this tedious task
VB Watch can insert error handlers in your code automatically. All you have to do is to select from the Plan Manager tab the local and global (if any) error handler code that you wish to insert. VB Watch comes with a number of predefined handlers that you may use as such or modify to your needs.
Besides, VB Watch can add line numbers and protect your code from exceptions (also known as GPF: General protection fault) as well.
Local error handlers
Local handlers are edited in plans directly.
Global error handlers
Global handlers are defined in the \VB Watch 2\Templates\VB6\Protector\Global Error Handler\ subdirectories.
Each subdirectory corresponds to a global error handler and may contain one or more code modules, forms, classes etc... The contents of the subdirectory is simply added to the protected project.
The title of the global error handler is the name of the subdirectory.
The description.txt file contains a short description of what the handler does, which is displayed in the VB Watch Control Center window.
Example: a simple global handler could have only one module file vbwErrorHandler.bas containing only one function (the one called in the local handler example):
Public Function vbwErrorHandler( _
ByVal ErrNumber As Long, _
ByVal ErrDescription As String, _
ByVal ErrProject As String, _
ByVal ErrSource As String, _
ByVal ErrProcedure As String, _
ByVal ErrLine As Long) As Long
'vbwNoErrorHandler ' don't remove this !
vbwErrorHandler = MsgBox( _
"Error " & ErrNumber & vbCrLf & _
"Description: " & ErrDescription & vbCrLf & _
"Project: " & ErrProject & vbCrLf & _
"Source: " & ErrSource & vbCrLf & _
"Procedure: " & ErrProcedure & vbCrLf & _
"Line: " & ErrLine & vbCrLf _
"Version: " & App.Major & "." & App.Minor & "." & App.Revision, _
vbAbortRetryIgnore + vbExclamation + vbMsgBoxSetForeground)
End Function
Note: VB Watch comes with a redistributable dll offering a number of advanced functions for error trapping that you may use in your global handlers. Please read carefully the paragraph on the ExcludedProperties property of this dll before using it.
Other added files
VB Watch also adds the \Templates\VB6\Protector\vbwProtector.bas template file that contains general code required for the error handling features.